package com.example.zuo.mysuperwidgetdemo.ui.refresh;

import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.zuo.mysuperwidgetdemo.R;
import com.example.zuo.mysuperwidgetdemo.adapter.superwrapper.ActionCenter;
import com.example.zuo.mysuperwidgetdemo.adapter.superwrapper.Builder;
import com.example.zuo.mysuperwidgetdemo.adapter.superwrapper.ConvertAction;
import com.example.zuo.mysuperwidgetdemo.adapter.superwrapper.FetchAction;
import com.example.zuo.mysuperwidgetdemo.adapter.superwrapper.SuperAdapterWrapper;
import com.example.zuo.mysuperwidgetdemo.ui.base.BaseNewActivity;
import com.example.zuo.mysuperwidgetdemo.widget.DividerItemDecoration;
import com.example.zuo.mysuperwidgetdemo.widget.SuperSwipeRefreshLayout;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

public class RecyclerviewActivity extends BaseNewActivity {
    //view
    SuperSwipeRefreshLayout refresh_layout;
    RecyclerView recyclerView;
    // Footer View
    private ProgressBar footerProgressBar;
    private TextView footerTextView;
    private ImageView footerImageView;

    //
    SuperAdapterWrapper wrapper;
    List<String> list = new ArrayList<>();

    @Override
    protected int bindLayout() {
        return R.layout.activity_recyclerview;
    }

    @Override
    protected void bindId() {
        refresh_layout = getView(R.id.refresh_layout);
        recyclerView = getView(R.id.recyclerView);
    }

    @Override
    protected void init() {
        refresh_layout.setFooterView(createFooterView());
        refresh_layout.setTargetScrollWithLayout(true);
        GetData();
    }

    private void ShowData() {
        Builder builder = new Builder(this);
        wrapper = builder.showIn(recyclerView)
                .defalutItemLayout(R.layout.item_main_layout)
                .addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL, R.color.white, 2))
                .addAction(ActionCenter.CONVERT_COMMAND, new ConvertAction() {
                    @Override
                    public void convert(ViewHolder holder, Object t, int position) {
                        holder.setText(R.id.name, (String) t);
                    }
                }).addAction(ActionCenter.FETCH_COMMAND, new FetchAction() {
                    @Override
                    public void fetch(boolean refresh) {
                        GetData();
                    }
                }).build();
        wrapper.handleData(list, true);
    }

    @Override
    protected void setListener() {

    }

    public void GetData() {
        new Handler().postDelayed(new Runnable() {
            public void run() {
                List<String> stringList = new ArrayList<>();
                for (int i = 0; i < 15; i++) {
                    stringList.add("my demo recyclerView" + i);
                }
                list = stringList;
                if (wrapper != null) {
                    wrapper.dataChange();
                } else {
                    ShowData();
                }

            }

        }, 3000);


    }

    private View createFooterView() {
        View footerView = LayoutInflater.from(refresh_layout.getContext())
                .inflate(R.layout.layout_footer, null);
        footerProgressBar = (ProgressBar) footerView
                .findViewById(R.id.footer_pb_view);
        footerImageView = (ImageView) footerView
                .findViewById(R.id.footer_image_view);
        footerTextView = (TextView) footerView
                .findViewById(R.id.footer_text_view);
        footerProgressBar.setVisibility(View.GONE);
        footerImageView.setVisibility(View.VISIBLE);
        footerImageView.setImageResource(R.mipmap.down_arrow);
        footerTextView.setText("上拉加载更多...");
        return footerView;
    }

}
